<?php
require 'vendor/autoload.php';

class DBConnector{
	
	private $dbHost ='127.0.0.1';
	private $userName;
	private $defaultUserName = 'user';
	private $defaultPasswd = 'password';
	private $passwd;
	private $dbName = 'uv1029_dengoDB';
	
	public function connectToDB(){
		$this->setCredentials();
		try {
		$dbConnection = new PDO("mysql:host=$this->dbHost;dbname=$this->dbName", $this->userName, $this->passwd);
		$dbConnection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
		$dbConnection-> exec('SET NAMES utf8'); 
		}
		catch(PDOException $e) {
			file_put_contents('errors/PDOErrors.txt', $e->getMessage(), FILE_APPEND);
		}
		return($dbConnection);
	}
	
	private function setCredentials(){
		$property = 'properties/creds.xml';
		if(file_exists($property)){
			$credentials = simplexml_load_file($property);	
			$this->userName=$credentials->DBcredential->userName;
			$this->passwd=$credentials->DBcredential->pass;
			$this->dbName=$credentials->DBcredential->dbName;
		}else{
			$this->userName=$this->defaultUserName;
			$this->passwd=$this->defaultPasswd;
		}
	}

}
?>
